package com.example.roadeyeserver.handler;

import cn.hutool.http.HttpRequest;
import com.example.roadeyeserver.entity.rq.ShipRQ;
import com.example.roadeyeserver.entity.rq.VehicleRQ;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * 省中心接口数据处理器
 */

@Component
@Slf4j
public class ProvinceCenterHandler {

    @Value("${provinceCenter.crossVehicleUrl}")
    private String crossVehicleUrl;

    @Value("${provinceCenter.crossVehicleKey}")
    private String crossVehicleKey;

    @Value("${provinceCenter.localVehicleUrl}")
    private String localVehicleUrl;

    @Value("${provinceCenter.localVehicleKey}")
    private String localVehicleKey;

    @Value("${provinceCenter.shipUrl}")
    private String shipUrl;

    @Value("${provinceCenter.shipKey}")
    private String shipKey;

    // 静态初始化 ObjectMapper 并配置忽略未知字段
    private static final ObjectMapper objectMapper = new ObjectMapper()
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    private final int timeOut = 10000;


    /**
     * 宁波跨域车辆GPS动态数据API查询服务（实时）
     * @return
     */
    public VehicleRQ getCrossVehicleData() {
        String requestUrl = crossVehicleUrl + "?appKey=" + crossVehicleKey;
        // http请求调用省中心接口
        String result = HttpRequest.get(requestUrl)
                .timeout(timeOut)
                .execute()
                .body();
        // 结果返回解析
        try {
            VehicleRQ vehicleRQ = objectMapper.readValue(result, VehicleRQ.class);
            if (vehicleRQ.getStatus() != 200) {
                log.error("调佣省中心接口:{},报错:{}", requestUrl, vehicleRQ);
            }
            return vehicleRQ;
        } catch (Exception e) {
            log.error("调佣省中心接口报错:{},结果:{},解析失败:{}", requestUrl, result, e.getMessage());
            e.printStackTrace();
            return new VehicleRQ();
        }
    }

    /**
     * 宁波本市车辆GPS动态数据API查询服务（实时）
     * @return
     */
    public VehicleRQ getLocalVehicleData() {
        String requestUrl = localVehicleUrl + "?appKey=" + localVehicleKey;
        // http请求调用省中心接口
        String result = HttpRequest.get(requestUrl)
                .timeout(timeOut)
                .execute()
                .body();
        // 结果返回解析
        try {
            VehicleRQ vehicleRQ = objectMapper.readValue(result, VehicleRQ.class);
            if (vehicleRQ.getStatus() != 200) {
                log.error("调佣省中心接口:{},报错:{}", requestUrl, vehicleRQ);
            }
            return vehicleRQ;
        } catch (Exception e) {
            log.error("调佣省中心接口报错:{},结果:{},解析失败:{}", requestUrl, result, e.getMessage());
            e.printStackTrace();
            return new VehicleRQ();
        }
    }

    /**
     * 全省船舶最新船舶定位动态数据API查询服务
     * @return
     */
    public ShipRQ getShipData() {
        String requestUrl = shipUrl + "?appKey=" + shipKey;
        // http请求调用省中心接口
        String result = HttpRequest.get(requestUrl)
                .timeout(timeOut)
                .execute()
                .body();
        // 结果返回解析
        try {
            ShipRQ shipRQ = objectMapper.readValue(result, ShipRQ.class);
            if (shipRQ.getStatus() != 200) {
                log.error("调佣省中心接口:{},报错:{}", requestUrl, shipRQ);
            }
            return shipRQ;
        } catch (Exception e) {
            log.error("调佣省中心接口报错:{},结果:{},解析失败:{}", requestUrl, result, e.getMessage());
            e.printStackTrace();
            return new ShipRQ();
        }
    }

//    @EventListener(ContextRefreshedEvent.class)
    public void init() {
        VehicleRQ vehicleRQ = this.getCrossVehicleData();
        log.info("跨域车辆数据:{}", vehicleRQ);
        VehicleRQ localVehicleData = this.getLocalVehicleData();
        log.info("本市车辆数据:{}", localVehicleData);
        ShipRQ shipData = this.getShipData();
        log.info("船舶数据:{}", shipData);
    }
}
